package com.elong.android.specialhouse.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.navi.BaiduMapAppNotSupportNaviException;
import com.baidu.mapapi.navi.BaiduMapNavigation;
import com.baidu.mapapi.navi.IllegalNaviArgumentException;
import com.baidu.mapapi.navi.NaviPara;
import com.baidu.mapapi.search.route.PlanNode;
import com.dp.android.elong.crash.LogWriter;
import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.base.BaseMapActivity;
import com.elong.android.specialhouse.utils.BDLocationManager;

/**
 * @author kaiqi.li
 * @Description 公寓地图页，导航
 * @date 2015/9/6 15:16
 */
public class ApartmentMapActivity extends BaseMapActivity {

    private LatLng startPoint ;
    private String startPlaceName ;
    private LatLng endPoint ;
    private String endPlaceName ;

    /**
     * init
     */
    protected void initContentView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.act_apartment_detail_map);
        findViewById(R.id.tv_apartment_detail_map_navigate).setOnClickListener(this);
        findViewById(R.id.iv_map_back).setOnClickListener(this);
        initData() ;
        drawApartmentOverlay();
    }

    private void drawApartmentOverlay() {
        try {
            animateToPoint(endPoint);
            View view = getLayoutInflater().inflate(R.layout.hotel_detail_map_hotel_overlay, null);
            drawHotelDetailsOverlay(endPoint, view);
            zoomToLevel(ZOOM_LEVEL_HOTEL_DETAILS);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    private void initData(){
        String lat = getIntent().getStringExtra(AppConstants.ACTIVITY_KEY_HOUSE_BAIDU_LATITUDE);
        String lng = getIntent().getStringExtra(AppConstants.ACTIVITY_KEY_HOUSE_BAIDU_LONGITUDE);
        if(lat != null && lng != null){
            endPoint = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng));
        }
        endPlaceName = getIntent().getStringExtra(AppConstants.ACTIVITY_KEY_HOUSE_ADDRESS);
        startPoint = BDLocationManager.getInstance().getLocation() ;
        startPlaceName = "我的位置" ;
    }

    private void onClickNav(){
         PlanNode startNode = PlanNode.withLocation(startPoint) ;
         PlanNode endNode = PlanNode.withLocation(endPoint) ;
         startNavigate("北京",startNode,endNode);
    }



    /**
     *
     * @Title: startNavigate
     * @Description: 百度地图导航
     * @param @param cityName
     * @param @param stNode
     * @param @param enNode
     * @return void
     * @throws
     */
    private void startNavigate(final String cityName, final PlanNode stNode, final PlanNode enNode) {
        NaviPara para = new NaviPara();
        para.startPoint = startPoint;
        para.startName = startPlaceName;
        para.endPoint = endPoint;
        para.endName = endPlaceName;

        try {

            BaiduMapNavigation.openBaiduMapNavi(para, this);

        } catch (BaiduMapAppNotSupportNaviException e) {
            LogWriter.logException(TAG, "", e);
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("您尚未安装百度地图app或app版本过低，点击确认安装？");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    BaiduMapNavigation.getLatestBaiduMapApp(ApartmentMapActivity.this);
                }
            });

            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
//                    mRoutePlanSearch.drivingSearch(new DrivingRoutePlanOption().from(stNode).to(enNode));
                    dialog.dismiss();
                }
            });

            builder.create().show();
        } catch (IllegalNaviArgumentException ex) {
            ex.printStackTrace();
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("地理位置信息缺失,暂时无法导航!");
            builder.setTitle("提示");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            builder.create().show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_apartment_detail_map_navigate:
                onClickNav();
                break;
            case R.id.iv_map_back:
                finish();
                break;
            default:
                break;
        }
    }
}
